package com.xjrsoft.module.form.utils;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.YesOrNoEnum;
import com.xjrsoft.common.model.generator.ComponentConfig;
import com.xjrsoft.common.utils.LocalDateTimeUtil;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.form.entity.FormDesignConfig;
import com.xjrsoft.module.generator.constant.ComponentTypeConstant;
import com.xjrsoft.module.generator.utils.GeneratorUtil;
import com.xjrsoft.module.magicapi.service.IMagicApiService;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.system.entity.Area;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.entity.Tree;
import com.xjrsoft.module.system.service.IAreaService;
import com.xjrsoft.module.system.service.ITreeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.ssssssss.magicapi.modules.db.model.PageResult;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public final class FormDataTransUtil {
    private FormDataTransUtil(){}

    private static final RedisUtil redisUtil;

    private static final IMagicApiService magicApiService;

    private static final IAreaService areaService;

    private static final ITreeService treeService;

    static {
        redisUtil = SpringUtil.getBean(RedisUtil.class);
        magicApiService = SpringUtil.getBean(IMagicApiService.class);
        areaService = SpringUtil.getBean(IAreaService.class);
        treeService = SpringUtil.getBean(ITreeService.class);
    }

//    /**
//     * 货币转大写所需,前端进行
//     */
//    private static final char[] CN_NUMBERS = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
//    private static final char[] CN_UNITS = {'分', '角', '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟'};


    public static void transData(List<Entity> dataList, FormDesignConfig formDesignConfig) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        List<DictionaryDetail> detailList = null;
        Map<String, Object> userMap = null;
        Map<String, Object> deptMap = null;
//        List<Map<String, Object>> postList = null;
        Map<String, Object> areaMap = null;
        Map<String, Map<String, Object>> apiDataMap = new HashMap<>();
        Map<String, Map<String, Object>> treeDataMap = new HashMap<>();
        List<ComponentConfig> componentConfigList = GeneratorUtil.getFormComponentListWithMain(formDesignConfig.getFormJson().getList());
        Map<String, Map<String, Object>> fieldValuesMap = new HashMap<>(componentConfigList.size());
        Map<String, Map<String, String>> multiFieldMap = new HashMap<>();
        Map<String, String> dateFormatMap = new HashMap<>(4);
        for (ComponentConfig componentConfig : componentConfigList) {
            String type = componentConfig.getType();
            String bindField = componentConfig.getBindField();
            Map<String, Object> options = componentConfig.getOptions();
            Integer infoType = MapUtils.getInteger(options, "infoType");
            boolean isCascade = StrUtil.equalsIgnoreCase(ComponentTypeConstant.CASCADE, type);
            if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.CHECKBOX, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.SELECT, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.ASSOCIATE_SELECT, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.ASSOCIATE_POPUP, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.MULTIPLE_POPUP, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.RADIO, type)
                    || isCascade) {
                String datasourceType = MapUtils.getString(options, "datasourceType");
                if (StrUtil.equalsIgnoreCase(datasourceType, "dic")) {
                    if (detailList == null) {
                        detailList = redisUtil.get(GlobalConstant.DIC_DETAIL_CACHE_KEY, new TypeReference<List<DictionaryDetail>>() {});
                    }
                    Map<String, Object> detailMap = detailList.stream().filter(x -> StrUtil.equalsIgnoreCase(x.getItemId().toString(), MapUtils.getString(options, "itemId"))).collect(Collectors.toMap(DictionaryDetail::getValue, DictionaryDetail::getName, (e1, e2) -> e1));
                    fieldValuesMap.put(bindField, detailMap);
                } else if (StrUtil.equalsIgnoreCase(datasourceType, "api") || isCascade) {
                    String apiId = MapUtils.getString(MapUtils.getMap(options, "apiConfig"), "apiId");
                    if (apiDataMap.get(apiId) == null) {
                        Object resultApiData = magicApiService.executeApi(apiId);
                        List<Map<String, Object>> apiDataList = null;
                        if (resultApiData instanceof PageResult) {
                            apiDataList = (List<Map<String, Object>>) ((PageResult) resultApiData).getList();
                        } else if (resultApiData instanceof List) {
                            apiDataList = (List<Map<String, Object>>) resultApiData;
                        }
                        if (isCascade) apiDataList = treeToList(apiDataList);
                        if (apiDataList != null ) apiDataMap.put(apiId, apiDataList.stream().filter(x -> ObjectUtil.isNotNull(x.get("value"))).collect(Collectors.toMap(data -> data.get("value").toString(), data -> data.get("label"), (e1, e2) -> e1)));
                    }
                    fieldValuesMap.put(bindField, apiDataMap.get(apiId));
                }
                if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.MULTIPLE_POPUP, type)
                        || StrUtil.equalsIgnoreCase(ComponentTypeConstant.CHECKBOX, type)
                        || isCascade
                        || (StrUtil.equalsIgnoreCase(type, ComponentTypeConstant.SELECT)
                                && MapUtils.getBoolean(options, "isMultiple", false))
                        || (StrUtil.equalsIgnoreCase(type, ComponentTypeConstant.TREE_SELECT)
                                && MapUtils.getBoolean(MapUtils.getMap(options, "treeConfig"), "isMultiple", false))) {
                    Map<String, String> multiSettingMap = new HashMap<>(4);
                    multiSettingMap.put("separator", MapUtils.getString(options, "separator", StringPool.COMMA));
                    multiSettingMap.put("showFormat", MapUtils.getString(options, "showFormat"));
                    multiFieldMap.put(bindField, multiSettingMap);
                }
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.AREA, type)) {
                Set<Long> areaValueList = new LinkedHashSet<>();
                for (Map<String, Object> data : dataList) {
                    String values = MapUtils.getString(data, bindField);
                    if (StrUtil.isEmpty(values)) continue;
                    String[] valueArray = values.split(StringPool.COMMA);
                    areaValueList.addAll(Arrays.stream(valueArray).map(NumberUtils::toLong).collect(Collectors.toSet()));
                }
                if (CollectionUtils.isNotEmpty(areaValueList)) {
                    List<Area> areaList = areaService.list(Wrappers.<Area>query().lambda().in(Area::getId, areaValueList));
                    areaMap = areaList.stream().collect(Collectors.toMap(area -> area.getId().toString(), Area::getName, (e1, e2) -> e1));
                } else {
                    areaMap = new HashMap<>(0);
                }
                fieldValuesMap.put(bindField, areaMap);
                multiFieldMap.put(bindField, null);
            }else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.ORGANIZATION, type)
                    || (StrUtil.equalsIgnoreCase(ComponentTypeConstant.INFO, type) && infoType.equals(1))) {
                if (deptMap == null) {
                    List<Department> departmentList = redisUtil.get(GlobalConstant.DEP_CACHE_KEY, new TypeReference<List<Department>>() {});
                    deptMap = departmentList.stream().collect(Collectors.toMap(department -> department.getId().toString(), Department::getName, (e1, e2) -> e1));
                }
                fieldValuesMap.put(bindField, deptMap);
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.USER, type)
                    || (StrUtil.equalsIgnoreCase(ComponentTypeConstant.INFO, type) && infoType.equals(0))) {
                if (userMap == null) {
                    List<User> userList = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {});
                    userMap = userList.stream().collect(Collectors.toMap(user -> user.getId().toString(), User::getName, (e1, e2) -> e1));
                }
                if (infoType == null) {
                    multiFieldMap.put(bindField, null);
                }
                fieldValuesMap.put(bindField, userMap);
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE, type)) {
                dateFormatMap.put(bindField, LocalDateTimeUtil.convertFontFormat(MapUtils.getString(options, "format")));
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE_RANGE, type)) {
                String format = LocalDateTimeUtil.convertFontFormat(MapUtils.getString(options, "format"));
                dateFormatMap.put(componentConfig.getBindStartTime(), format);
                dateFormatMap.put(componentConfig.getBindEndTime(), format);
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.TREE_SELECT, type)) {
                String treeId = MapUtils.getString(MapUtils.getMap(options, "treeConfig"), "id");
                if (treeDataMap.get(treeId) != null) {
                    Tree treeConfig = treeService.getById(Long.valueOf(treeId));
                    JSONObject configJson = JSON.parseObject(treeConfig.getConfig());
                    Integer treeType = treeConfig.getType();
                    Map<String, Object> map = new HashMap<>();
                    if (treeType == 1) {
                        // 静态数据
                        JSONArray staticData = configJson.getJSONArray("staticData");
                        buildTreeMap(staticData, "label", "value", StrUtil.EMPTY, map);
                    } else if (treeType == 2) {
                        // API数据
                        JSONObject apiData = configJson.getJSONObject("apiData");
                        JSONObject apiConfig = apiData.getJSONObject("apiConfig");
                        String apiId = apiConfig.getString("id");
                        Map<String, Object> data = magicApiService.executeApi(apiId);

                        JSONArray apiColumns = apiData.getJSONArray("apiColumns");
                        String label = ((JSONObject) apiColumns.get(0)).getString("dataIndex");
                        String value = ((JSONObject) apiColumns.get(1)).getString("dataIndex");

                        buildTreeMap((JSONArray) JSONArray.toJSON(data.get("data")), label, value, StrUtil.EMPTY, map);

                    }
                    treeDataMap.put(treeId, map);
                    fieldValuesMap.put(bindField, treeDataMap.get(treeId));
                    if (MapUtils.getBoolean(MapUtils.getMap(options, "treeConfig"), "isMultiple", false)) {
                        Map<String, String> multiSettingMap = new HashMap<>(2);
                        multiSettingMap.put("separator", MapUtils.getString(options, "separator", StringPool.COMMA));
                        multiFieldMap.put(bindField, multiSettingMap);
                    }
                }
            }
        }

        for (Map<String, Object> data : dataList) {
            for (Map.Entry<String, Map<String, Object>> entry : fieldValuesMap.entrySet()) {
                String key = entry.getKey();
                Object o = data.get(key);
                if (StrUtil.isEmptyIfStr(o)) {
                    continue;
                }
                String value = String.valueOf(o);
                Map<String, Object> entryValue = entry.getValue();
                if (multiFieldMap.containsKey(key)) {
                    List<String> values = StrUtil.split(value, StringPool.COMMA);
                    Map<String, String> multiSettingsMap = multiFieldMap.get(key);
                    StringBuilder showValue = new StringBuilder();
                    for (int i = 0; i < values.size(); i++) {
                        String valueStr = values.get(i);
                        String separator = StringPool.COMMA;
                        Object showValueStr = entryValue.get(valueStr);
                        if (StrUtil.isEmptyIfStr(showValueStr)) continue;
                        if (CollectionUtils.isNotEmpty(multiSettingsMap)) {
                            separator = MapUtils.getString(multiSettingsMap, "separator", StringPool.COMMA);
                            String showFormat = MapUtils.getString(multiSettingsMap, "showFormat");
                            if (StrUtil.equalsIgnoreCase(showFormat, "showMostChildLevel")) {
                                if (i == values.size() - 1) {
                                    showValue.append(showValueStr);
                                }
                                continue;
                            } else {
                                showValue.append(showValueStr);
                            }
                        } else {
                            showValue.append(showValueStr);
                        }
                        if (i < values.size() - 1) {
                            showValue.append(separator);
                        }
                    }
                    if (showValue.length() > 0) data.put(key, showValue.toString());
                } else {
                    Object obj = entryValue.get(value);
                    if (StrUtil.isEmptyIfStr(obj)) continue;
                    data.put(key, obj);
                }
            }
            // 时间格式化处理
            for (Map.Entry<String, String> entry : dateFormatMap.entrySet()) {
                String key = entry.getKey();
                Object value = data.get(key);
                if (value == null) {
                    continue;
                }
                if (value instanceof Timestamp) {
                    Timestamp timestamp = (Timestamp) value;
                    data.put(key, LocalDateTimeUtil.format(timestamp.toLocalDateTime(), entry.getValue()));
                }
            }
        }
    }

    public static void transDataOver(List<Entity> dataList, FormDesignConfig formDesignConfig,Integer formType) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        List<DictionaryDetail> detailList = null;
        Map<String, Object> userMap = null;
        Map<String, Object> deptMap = null;
//        List<Map<String, Object>> postList = null;
        Map<String, Object> areaMap = null;
        Map<String, Map<String, Object>> apiDataMap = new HashMap<>();
        List<ComponentConfig> componentConfigList = GeneratorUtil.getFormComponentListWithMain(formDesignConfig.getFormJson().getList());
        Map<String, Map<String, Object>> fieldValuesMap = new HashMap<>(componentConfigList.size());
        Map<String, Map<String, String>> multiFieldMap = new HashMap<>();
        Map<String, String> dateFormatMap = new HashMap<>(4);
        List<String> intFieldList = new ArrayList<>();
        List<String> keyList = new ArrayList<>(dataList.size());
        for (ComponentConfig componentConfig : componentConfigList) {
            String type = componentConfig.getType();
            String bindField = componentConfig.getBindField();
            keyList.add(bindField);
            Map<String, Object> options = componentConfig.getOptions();
            Integer infoType = MapUtils.getInteger(options, "infoType");
            boolean isCascade = StrUtil.equalsIgnoreCase(ComponentTypeConstant.CASCADE, type);
            if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.CHECKBOX, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.SELECT, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.ASSOCIATE_SELECT, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.ASSOCIATE_POPUP, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.MULTIPLE_POPUP, type)
                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.RADIO, type)
                    || isCascade) {
                String datasourceType = MapUtils.getString(options, "datasourceType");
                if (StrUtil.equalsIgnoreCase(datasourceType, "dic")) {
                    if (detailList == null) {
                        detailList = redisUtil.get(GlobalConstant.DIC_DETAIL_CACHE_KEY, new TypeReference<List<DictionaryDetail>>() {});
                    }
                    Map<String, Object> detailMap = detailList.stream().filter(x -> StrUtil.equalsIgnoreCase(x.getItemId().toString(), MapUtils.getString(options, "itemId"))).collect(Collectors.toMap(DictionaryDetail::getValue, DictionaryDetail::getName, (e1, e2) -> e1));
                    fieldValuesMap.put(bindField, detailMap);
                } else if (StrUtil.equalsIgnoreCase(datasourceType, "api") || isCascade) {
                    String apiId = MapUtils.getString(MapUtils.getMap(options, "apiConfig"), "apiId");
                    if (apiDataMap.get(apiId) == null) {
                        Object resultApiData = magicApiService.executeApi(apiId);
                        List<Map<String, Object>> apiDataList = null;
                        if (resultApiData instanceof PageResult) {
                            apiDataList = (List<Map<String, Object>>) ((PageResult) resultApiData).getList();
                        } else if (resultApiData instanceof List) {
                            apiDataList = (List<Map<String, Object>>) resultApiData;
                        }
                        if (isCascade) apiDataList = treeToList(apiDataList);
                        if (apiDataList != null ) apiDataMap.put(apiId, apiDataList.stream().filter(x -> ObjectUtil.isNotNull(x.get("value"))).collect(Collectors.toMap(data -> data.get("value").toString(), data -> data.get("label"), (e1, e2) -> e1)));
                    }
                    fieldValuesMap.put(bindField, apiDataMap.get(apiId));
                }
                if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.MULTIPLE_POPUP, type)
                        || StrUtil.equalsIgnoreCase(ComponentTypeConstant.CHECKBOX, type)
                        || isCascade) {
                    Map<String, String> multiSettingMap = new HashMap<>(4);
                    multiSettingMap.put("separator", MapUtils.getString(options, "separator", StringPool.COMMA));
                    multiSettingMap.put("showFormat", MapUtils.getString(options, "showFormat"));
                    multiFieldMap.put(bindField, multiSettingMap);
                }
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.AREA, type)) {
                Set<Long> areaValueList = new LinkedHashSet<>();
                for (Map<String, Object> data : dataList) {
                    String values = MapUtils.getString(data, bindField);
                    if (StrUtil.isEmpty(values)) continue;
                    String[] valueArray = values.split(StringPool.COMMA);
                    areaValueList.addAll(Arrays.stream(valueArray).map(NumberUtils::toLong).collect(Collectors.toSet()));
                }
                if (CollectionUtils.isNotEmpty(areaValueList)) {
                    List<Area> areaList = areaService.list(Wrappers.<Area>query().lambda().in(Area::getId, areaValueList));
                    areaMap = areaList.stream().collect(Collectors.toMap(area -> area.getId().toString(), Area::getName, (e1, e2) -> e1));
                } else {
                    areaMap = new HashMap<>(0);
                }
                fieldValuesMap.put(bindField, areaMap);
                Map<String, String> multiSettingMap = new HashMap<>(4);
                if (formType == YesOrNoEnum.YES.getCode()){//自定义表单
                    multiSettingMap.put("separator",  StringPool.COMMA);
                }else {
                    multiSettingMap.put("separator",  StringPool.SLASH);
                }
                multiSettingMap.put("showFormat", null);
                multiFieldMap.put(bindField, multiSettingMap);
//                multiFieldMap.put(bindField, null);
            }else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.ORGANIZATION, type)
                    || (StrUtil.equalsIgnoreCase(ComponentTypeConstant.INFO, type) && infoType.equals(1))) {
                if (deptMap == null) {
                    List<Department> departmentList = redisUtil.get(GlobalConstant.DEP_CACHE_KEY, new TypeReference<List<Department>>() {});
                    deptMap = departmentList.stream().collect(Collectors.toMap(department -> department.getId().toString(), Department::getName, (e1, e2) -> e1));
                }
                fieldValuesMap.put(bindField, deptMap);
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.USER, type)
                    || (StrUtil.equalsIgnoreCase(ComponentTypeConstant.INFO, type) && infoType.equals(0))) {
                if (userMap == null) {
                    List<User> userList = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {});
                    userMap = userList.stream().collect(Collectors.toMap(user -> user.getId().toString(), User::getName, (e1, e2) -> e1));
                }
                if (infoType == null) {
                    multiFieldMap.put(bindField, null);
                }
                fieldValuesMap.put(bindField, userMap);
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE, type)) {
                dateFormatMap.put(bindField, LocalDateTimeUtil.convertFontFormat(MapUtils.getString(options, "format")));
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE_RANGE, type)) {
                String format = LocalDateTimeUtil.convertFontFormat(MapUtils.getString(options, "format"));
                dateFormatMap.put(componentConfig.getBindStartTime(), format);
                dateFormatMap.put(componentConfig.getBindEndTime(), format);
            }
            else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.MONEY_CHINESE, type)) {
                intFieldList.add(bindField);
            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.NUMBER, type)) {
                intFieldList.add(bindField);
            }
        }

        for (Map<String, Object> data : dataList) {
            for (String key : keyList) {
                String addKey = key + StringPool.UNDERSCORE + "api";
                Object o = data.get(key);
                String value = String.valueOf(o);
                data.put(addKey,value);
            }
            for (Map.Entry<String, Map<String, Object>> entry : fieldValuesMap.entrySet()) {
                String key = entry.getKey();
                Object o = data.get(key);
                if (StrUtil.isEmptyIfStr(o)) {
                    continue;
                }
                String value = String.valueOf(o);
                Map<String, Object> entryValue = entry.getValue();
                if (multiFieldMap.containsKey(key)) {
                    List<String> values = StrUtil.split(value, StringPool.COMMA);
                    Map<String, String> multiSettingsMap = multiFieldMap.get(key);
                    StringBuilder showValue = new StringBuilder();
                    for (int i = 0; i < values.size(); i++) {
                        String valueStr = values.get(i);
                        String separator = StringPool.COMMA;
                        Object showValueStr = entryValue.get(valueStr);
                        if (StrUtil.isEmptyIfStr(showValueStr)) continue;
                        if (CollectionUtils.isNotEmpty(multiSettingsMap)) {
                            separator = MapUtils.getString(multiSettingsMap, "separator", StringPool.COMMA);
                            String showFormat = MapUtils.getString(multiSettingsMap, "showFormat");
                            if (StrUtil.equalsIgnoreCase(showFormat, "showMostChildLevel")) {
                                if (i == values.size() - 1) {
                                    showValue.append(showValueStr);
                                }
                                continue;
                            } else {
                                showValue.append(showValueStr);
                            }
                        } else {
                            showValue.append(showValueStr);
                        }
                        if (i < values.size() - 1) {
                            showValue.append(separator);
                        }
                    }
                    if (showValue.length() > 0) data.put(key, showValue.toString());
                } else {
                    Object obj = entryValue.get(value);
                    if (StrUtil.isEmptyIfStr(obj)) continue;
                    data.put(key, obj);
                }
            }
            // 时间格式化处理
            for (Map.Entry<String, String> entry : dateFormatMap.entrySet()) {
                String key = entry.getKey();
                Object value = data.get(key);
                if (value == null) {
                    continue;
                }
                if (value instanceof Timestamp) {
                    Timestamp timestamp = (Timestamp) value;
                    data.put(key, LocalDateTimeUtil.format(timestamp.toLocalDateTime(), entry.getValue()));
                }
            }
            if (CollectionUtils.isNotEmpty(intFieldList)){
                for (String key : intFieldList) {
                    Object value = data.get(key);
                    if (value == null) {
                        continue;
                    }
                    //货币和计数组件，去除后面的0
                    String valueString = NumberUtil.toStr(Double.valueOf(value.toString()));
                    //数据添加
                    data.put(key,valueString);
                }
            }
            if (formType == YesOrNoEnum.NO.getCode()){//系统表单.转换成驼峰数据
                for (String key : keyList) {
                    String camelKey = com.baomidou.mybatisplus.core.toolkit.StringUtils.underlineToCamel(key);;
                    Object o = data.get(key);
                    String value = String.valueOf(o);
                    //先移除与那边的数据，再新增，防止本来就是驼峰的数据key数据被误删
                    data.remove(key);
                    data.put(camelKey,value);
                }
            }
        }
    }

    /**
     * 将string类型的金额转为对应的大写金额（保留两位小数）
     * @param amount 金额
     * @return
     */
//    private static StringBuilder convertMoney(String amount){
//        //保留两位小数
//        amount = NumberUtil.roundStr(amount, 2);
//        double amountDouble = Double.parseDouble(amount);
//        StringBuilder result = new StringBuilder();
//        //转大写
//        int yuan = (int) Math.floor(amountDouble);
//        int jiao = (int) Math.floor(amountDouble * 10 % 10); // 角
//        int fen = (int) Math.floor(amountDouble * 100 % 10); // 分
//        if (yuan > 0) {
//            String yuanStr = String.valueOf(yuan);
//            for (int i = 0; i < yuanStr.length(); i++) {
//                result.append(CN_NUMBERS[yuanStr.charAt(i) - '0']);
//                result.append(CN_UNITS[yuanStr.length() - 1 - i + 2]);
//            }
//        } else {
//            result.append(CN_NUMBERS[0]);
//            result.append(CN_UNITS[2]); // 元
//        }
//
//        if (jiao > 0) {
//            result.append(CN_NUMBERS[jiao]);//角
//            result.append(CN_UNITS[1]);
//        }
//
//        if (fen > 0) {
//            result.append(CN_NUMBERS[fen]);
//            result.append(CN_UNITS[0]); // 分
//        }
//        return result;
//    }

    private static List<Map<String, Object>> treeToList(List<Map<String, Object>> treeDataList) {
        if (CollectionUtils.isEmpty(treeDataList)) {
            return treeDataList;
        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map<String, Object> data : treeDataList) {
            resultList.add(data);
            Object obj = data.get("children");
            if (obj instanceof List) {
                resultList.addAll(treeToList((List<Map<String, Object>>)obj));
            }
        }
        return resultList;
    }

    private static void buildTreeMap(JSONArray list, String label, String value, String parentValue, Map<String, Object> resultMap) {
        for (Object obj : list) {
            JSONObject data = (JSONObject) obj;
            String dataValue = MapUtils.getString(data, value);
            String fullDataValue = StrUtil.isBlank(parentValue) ? dataValue : parentValue + StringPool.DASH + StringPool.DASH + StringPool.DASH + dataValue;
            resultMap.put(fullDataValue, data.getString(label));
            JSONArray children = data.getJSONArray("children");
            if (CollectionUtils.isNotEmpty(children)) {
                buildTreeMap(children, label, value, fullDataValue, resultMap);
            }
        }
    }
}
